import { ApiProperty } from '@nestjs/swagger'
import { Type } from 'class-transformer'
import { IsArray, IsBoolean, IsNumber, IsOptional, IsString, ValidateNested } from 'class-validator'
import { Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm'

/**
 * 问卷管理
 */
@Entity('question')
export class Question {
  @PrimaryGeneratedColumn()
  @IsNumber()
  questionId?: number

  /**
   * 问卷名称
   */
  @Column()
  @IsString()
  paperName: string

  /**
   * 题目列表
   */
  @Column({ type: 'json' })
  @IsArray()
  @IsOptional()
  @ValidateNested({ each: true })
  @Type(() => QuestionItem)
  questionList?: QuestionItem[]

  /**
   * 是否启用
   */
  @ApiProperty({ default: true })
  @Column({ default: true })
  @IsBoolean()
  @IsOptional()
  isEnabled?: boolean

  @CreateDateColumn()
  createdAt?: Date

  @UpdateDateColumn()
  updatedAt?: Date
}

export class QuestionItem {
  /**
   * 问题
   */
  @IsString()
  questionLabel: string

  /**
   * 选项
   */
  @IsArray()
  @IsString({ each: true })
  questionOption: string[]

  /**
   * 正确的选项
   */
  @IsString()
  correctOption: string
}
